var vm = new Vue({
    el: '#SysUseChangePassword',
    data: {
        msg: '',
        isDebug: true,
        show:false,

        sysUserId: userId,
        userPhone: '',
        userName: '',
        oldPwd: '',
        newPwd: '',
        newPwd2: ''
    },
    created: function () {
        var json = JSON.parse('{ "id": "" }');
        json.id = this.sysUserId;

        if(this.sysUserId == ''){
            this.showMsg("会话超时,请重新登录!",true);
            return;
        }

        this.$http.post('/mgr/SysUserCtrl/getSysUserById', json).then(response => {

            var data = response.body;

        if (data.resultCode != '200') {
            this.showMsg(data.msg, true);
            return;
        }

        if (data.result == null || data.result.length == 0) {
            return;
        }

        this.userPhone = data.result.name;
        this.userName = data.result.realName;

        if(this.isDebug)
            console.log(response);

        this.show=true;
    })
    },
    methods: {

        save: function () {
            if(this.validatePasswordErro()){
               return;
            }

            var json = JSON.parse('{ "id": "", "oldPwd": "", "newPwd": "" }');
            json.id = this.sysUserId;
            json.oldPwd = this.oldPwd;
            json.newPwd = this.newPwd;


            if (this.isDebug)
                console.log(JSON.stringify(json));

            this.$http.post('/mgr/SysUserCtrl/changeSysUserPwd', json).then(
                response => {

                var data = response.body;

            if (data.resultCode != '200') {
                this.showMsg(data.msg, true);
                return;
            }

            if (data.result == null || data.result.length == 0) {
                this.showMsg('保存失败', true);
                return;
            }

            this.showMsg(data.msg);
        })
        },
        validatePasswordErro: function () {
            console.log(this.sysUserId);

            if(this.sysUserId == ''){
                this.showMsg("会话超时,请重新登录!",true);
                return true;
            }

            if(this.oldPwd ==''
               || this.newPwd ==''
               || this.newPwd2 ==''
            ){
                this.showMsg("请输入新旧密码!",true);
                return true;
            }

            let regExp = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/;

            if(regExp.test(this.newPwd) === false){
                this.showMsg("密码格式不正确，请输入6-12位密码，必须字母加数字!",true);
                this.clearInput();
                return true;
            }

            if(this.newPwd != this.newPwd2){
                this.showMsg("新密码,确认密码不一致!",true);
                return true;
            }

            if(this.newPwd == this.oldPwd){
                this.showMsg("新密码不能和旧密码相同!",true);
                return true;
            }
            return false;
        },

        showMsg: function (msg, isAlertParam) {
            layer.alert(msg);
            this.clearInput();
        } ,
        clearInput: function(){
            this.oldPwd="";
            this.newPwd="";
            this.newPwd2="";
        }
    }
})



